package cn.edu.sdjzu.xg2121.service;

import cn.edu.sdjzu.xg2121.dao.TeacherDao;
import cn.edu.sdjzu.xg2121.domain.Department;
import cn.edu.sdjzu.xg2121.domain.Teacher;
import cn.edu.sdjzu.xg2121.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TeacherService {
    static TeacherDao teacherDao = new TeacherDao();
    private static TeacherService teacherService = null;
    public static TeacherService getInstance(){
        if (teacherService == null){
            teacherService = new TeacherService();
        }
        return teacherService;
    }
    public Teacher find(int department_id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        return TeacherDao.getInstance().find(department_id,connection);
    }
    public Teacher add(Teacher teacher) throws SQLException {
        teacher = teacherDao.add(teacher);
        return teacher;
    }
    public Teacher delete(Teacher teacher) throws SQLException {
        teacher = teacherDao.delete(teacher);
        return teacher;
    }
    public Teacher update(Teacher teacher) throws SQLException {
        Department department=DepartmentService.getInstance().find(teacher.getDepartment().getId());
        if (department==null){
            return null;
        }
        teacher = teacherDao.update(teacher);
        return teacher;
    }
}
